package com.paul.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import javax.naming.directory.SearchControls;
import javax.persistence.criteria.CriteriaBuilder.In;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSON;
import com.domain.User;
import com.paul.service.UserService;
import com.paul.service.UserServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.support.WebApplicationContextUtils;

@Controller
public class UserServlet extends HttpServlet {

	// public UserService userService = getService();
	// public UserService userService = new UserService();
	
	@Autowired
	public UserService userService;
	
	public UserService getUserService() {

		ServletContext servletContext = getServletContext();
		
		ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
		
		UserService userService = (UserServiceImpl) context.getBean("userService");

		return userService;

	}
	
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		System.out.println("进入servlet控制�?");
		response.setContentType("text/html;charset=gbk");

		String methodName = request.getParameter("methodName");

		int x = Integer.parseInt(methodName);
		System.out.println(x);

		try {

			switch (x) {
			case 1:
				System.out.println("已经到这里了");
				checkUser(request, response);
				break;
			case 2:

				showUser(request, response);
				break;

			case 3:
				editUser(request, response);
				break;

			case 4:
				deleteUser(request, response);
				break;

			case 5:
				insertUser(request, response);
				break;

			default:
				break;
			}

			System.out.println("servlet处理完毕!");
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("servlet报异�?");
			e.printStackTrace();
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		this.doGet(request, response);

	}

	public void showUser(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		
		UserService userService = getUserService();
		// 接收
		String username = request.getParameter("username");
		String email = request.getParameter("email");
		String page = request.getParameter("page");
		String rows = request.getParameter("rows");

		try {

			Map<String, Object> a = userService.showUser(username, email, page, rows);

			int total = (Integer) a.get("total");

			String row = (String) a.get("rows");

			response.getWriter().println("{\"total\":" + total + ",\n\"rows\":" + row + "\n}");
		} catch (Exception e) {
			System.out.println("servlet的showUser报异常了");
			// TODO: handle exception
		}
	}

	public void searchUser(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		UserService userService = getUserService();
		// 接收
		String username = request.getParameter("username");
		String email = request.getParameter("email");

		String page = request.getParameter("page");
		String rows = request.getParameter("rows");

		Map<String, Object> a = userService.showUser(username, email, page, rows);

		String total = (String) a.get("total");
		String row = (String) a.get("rows");

		response.getWriter().println("{\"total\":" + total + ",\n\"rows\":" + row + "\n}");

	}


	public void checkUser(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		// 插入spring
		UserService userService = getUserService();

		System.out.println("加入接口的程序已经到这里�?");
		

		// 插入spring
		String username = request.getParameter("username");
		String password = request.getParameter("password");

		if (userService.checkUser(username, password)) {
			// 合法用户,跳至欢迎界面
			request.getRequestDispatcher("welcome.jsp").forward(request, response);

		} else {
			// 非法用户,返回登录界面
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}
	}

	public void editUser(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		UserService userService = getUserService();
		String userJsonString = request.getParameter("userJsonString");
		User user = JSON.parseObject(userJsonString, User.class);
		
		
		if (userService.editUser(user)) {
			// 编辑用户成功
			System.out.println("编辑用户信息成功!");
			response.getWriter().print(1);

		} else {
			// 编辑用户失败
			System.out.println("编辑用户信息失败!!!");
			response.getWriter().print(0);
		}	
	}

	public void deleteUser(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		UserService userService = getUserService();
		// 接收
		String userId = request.getParameter("id");

		if (userService.deleteUser(userId)) {
			// 删除用户成功
			System.out.println("删除用户信息成功!");
			response.getWriter().print(1);

		} else {
			// 编辑用户失败
			System.out.println("删除用户信息失败!!!");
			response.getWriter().print(0);
		}

	}

	public void insertUser(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		UserService userService = getUserService();
		String userJsonString = request.getParameter("userJsonString");

		User user = JSON.parseObject(userJsonString, User.class);

		if (userService.insertUser(user)) {
			// 插入用户成功
			System.out.println("插入用户信息成功!");
			response.getWriter().print(1);

		} else {
			// 编辑用户失败
			System.out.println("插入用户信息失败!!!");
			response.getWriter().print(0);
		}

	}
}
